package cn.stylefeng.guns.modular.category.service.impl;

import cn.stylefeng.guns.core.common.node.ZTreeNode;
import cn.stylefeng.guns.modular.system.dao.CategoryMapper;
import cn.stylefeng.guns.modular.system.model.Category;
import cn.stylefeng.guns.modular.system.dao.CategoryMapper;
import cn.stylefeng.guns.modular.category.service.ICategoryService;
import cn.stylefeng.guns.modular.system.model.Category;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品类别表 服务实现类
 * </p>
 *
 * @author baikun
 * @since 2020-04-16
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    @Override
    @Transactional
    public void deleteCategory(Integer categoryId) {
        Category category = categoryMapper.selectById(categoryId);

        Wrapper<Category> wrapper = new EntityWrapper<>();
        wrapper = wrapper.like("pids", "%[" + category.getId() + "]%");
        List<Category> subCategorys = categoryMapper.selectList(wrapper);
        for (Category temp : subCategorys) {
            temp.deleteById();
        }

        category.deleteById();
    }

    @Override
    public List<ZTreeNode> tree() {
        return this.baseMapper.tree();
    }

    @Override
    public List<Map<String, Object>> list(String condition) {
        return this.baseMapper.list(condition);
    }
}
